package com.powernode.controller;

import com.github.pagehelper.Page;
import com.powernode.common.Constants;
import com.powernode.common.Result;
import com.powernode.domain.User;
import com.powernode.dto.UserDto;
import com.powernode.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("user")
public class UserController {
    @Resource  //注入userService
    private UserService userService;

    //根据id来查询一个用户
    @RequestMapping("queryUserById.action")
    public User queryUserById(Integer id) {
        return userService.selectByPrimaryKey(id);
    }

    // 分页查询
    @GetMapping("query.action")
    public Result query(UserDto userDto) {
        Page<User> page = this.userService.queryForPage(userDto);
        return new Result(0, "查询成功", page.getTotal(), page.getResult());
    }
    // 添加
    @PostMapping("add.action")
    public Result add(User user){
        //设置密码
        user.setPassword(Constants.USER_DEFAULT_PWD);
        //设置在职状态
        user.setDeleted(Constants.USER_DELETED_1);
        //设置默认头像
        user.setImg(Constants.USER_DEFAULT_IMG);
        //设置创建时间
        user.setCreateTime(new Date());
        //保存
        try {
            this.userService.insertSelective(user);
            return new Result(200,"添加成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(-1,"添加失败");
        }
    }

    // 更新
    @PostMapping("update.action")
    public Result update(User user){
        try {
            this.userService.updateByPrimaryKeySelective(user);
            return new Result(200,"修改成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(-1,"修改失败");
        }
    }
    // 离职
    @PostMapping("del.action")
    public Result del(Integer id){
        User user =new User();
        user.setId(id);
        user.setDeleted(Constants.USER_DELETED_2);
        user.setDeletedTime(new Date());
        try {
            this.userService.updateByPrimaryKeySelective(user);
            return new Result(200,"离职成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(-1,"离职失败");
        }
    }
    // 重置密码
    @PostMapping("resetPwd.action")
    public Result resetPwd(Integer id){
        User user =new User();
        user.setId(id);
        user.setPassword(Constants.USER_DEFAULT_PWD);
        try {
            this.userService.updateByPrimaryKeySelective(user);
            return new Result(200,"重置密码成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(-1,"重置密码失败");
        }
    }

    /**
     * 查询所有在职的业务员
     */
    @RequestMapping("queryAllAvailableUser.action")
    public Result queryAllAvailableUser(){
        Integer deleted=Constants.USER_DELETED_1;
        Integer role=Constants.USER_TYPE_NORMAL;
        List<User> userList=this.userService.queryUsers(deleted,role);
        return new Result(200,"查询成功",0L,userList);
    }
    /**
     * 验证当前用户的密码
     */
    @PostMapping("checkOldPwd.action")
    public  Result checkOldPwd(String oldPwd, HttpSession session){
        //从session里面得到用户ID
        User sessionUser= (User) session.getAttribute(Constants.SESSION_USER_KEY);
        //根据用户ID查询用户
        User user=this.userService.selectByPrimaryKey(sessionUser.getId());
        if(DigestUtils.md5DigestAsHex(oldPwd.getBytes()).equals(user.getPassword())){
            return new Result(200,"密码验证成功");
        }
        return new Result(-1,"密码验证失败");
    }

    /**
     * 修改密码
     */
    @PostMapping("changeUserPwd.action")
    public  Result changeUserPwd(String newPwd, HttpSession session){
        try {
            //从session里面得到用户ID
            User sessionUser= (User) session.getAttribute(Constants.SESSION_USER_KEY);
            //根据用户ID查询用户
            User user=this.userService.selectByPrimaryKey(sessionUser.getId());
            user.setPassword(DigestUtils.md5DigestAsHex(newPwd.getBytes()));
            this.userService.updateByPrimaryKeySelective(user);
            return new Result(200,"密码修改成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(-1,"密码修改失败");
        }
    }

}
